package com.hch.web;

import com.hch.domain.Type;
import com.hch.service.BlogService;
import com.hch.service.TypeService;
import com.hch.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;


    //获取分类id对应的博客内容
    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, @PathVariable Long id, Model model) {
        //获取所有分类,设置足够大的数值就能取到所有的分类
        List<Type> types = typeService.listTypeTop(100);
        if (id == -1) { //-1为初始值，默认从首页点击进入分类页
            id = types.get(0).getId();
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        //将所有分类存入model
        model.addAttribute("types", types);
        //将分类的博客存入model,根据分类分页查询博客信息
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
        //将活动的分类存入model
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
